
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ExpansionTitleSample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('ExpansionTile'),
        ),
        body: new ListView.builder(
          itemBuilder: (BuildContext context, int index) => new EntryItem(data[index]),
          itemCount: data.length,
        ),
      ),
    );
  }
}

class Entry{
  Entry(this.title,[this.children=const <Entry>[]]);
  final String title;
  final List<Entry> children;
}

final List<Entry> data = <Entry>[
  Entry('Chapter A',
  <Entry>[
    Entry('Section A0',
      <Entry>[
        Entry('Item A0.1'),
        Entry('Item A0.2'),
        Entry('Item A0.3')
      ]),
    Entry('Section A1'),
    Entry('Section A2'),
  ]),
  Entry('Chapter B',
      <Entry>[
        Entry('Section B0',
            <Entry>[
              Entry('Item B0.1'),
              Entry('Item B0.2'),
              Entry('Item B0.3')
            ]),
        Entry('Section B1'),
        Entry('Section B2'),
      ]),
  Entry('Chapter C',
      <Entry>[
        Entry('Section C0',
            <Entry>[
              Entry('Item C0.1'),
              Entry('Item C0.2'),
              Entry('Item C0.3')
            ]),
        Entry('Section C1'),
        Entry('Section C2'),
      ]),
  Entry('Chapter D',
      <Entry>[
        Entry('Section D0',
            <Entry>[
              Entry('Item D0.1'),
              Entry('Item D0.2'),
              Entry('Item D0.3')
            ]),
        Entry('Section D1'),
        Entry('Section D2'),
      ])
];

class EntryItem extends StatelessWidget {
  const EntryItem(this.entry);
  final Entry entry;

  Widget _buildTiles(Entry root){
    if(root.children.isEmpty){
      return ListTile(title: Text(root.title),);
    }
    return ExpansionTile(
      key:PageStorageKey<Entry>(root),
      title:Text(root.title),
      children: root.children.map(_buildTiles).toList(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return _buildTiles(entry);
  }
}

